<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./lib/vue.js"></script>
    <link rel="stylesheet" href="./lib/bootstrap-4.1.3-dist/css/bootstrap.min.css">
</head>
<body>
   
    <div id="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
                </div>
                <div class="panel-body form-inline">
                    <label>
                        Id:
                        <input type="text" class="from-control" v-model="id">
                    </label>
                    <label>
                        name:
                        <input type="text" class="from-control" v-model="name">
                    </label>
                    
                        <input type="button" value="添加" class="btn btn-primary" @click="add()">
                        <label>
                        搜索关键字：
                            <input type="text" v-model="keywords" @change="searchByName">
                            
                            </label>
                           
                    
                    </div>
                    </div>
        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item in searchByName()" :key="item.id">
                        <td>{{ item.id }}</td>
                        <td v-text="item.name"></td>
                        <td>{{ item.ctime|formatDate() }}</td>
                        <td>
                            <a href="" @click.prevent="del(item.id)">删除</a>
                        </td>
                        
                        </tr>
                        </tbody>
                        </table>


    </div>

    <script >
         /* var searchByName; */
        Vue.filter('formatDate',function(data){
            console.log("过滤器被调用了")
            return data+'---'

        })
    
       var vm= new Vue({
            el: '#app',
            data:{
                id:'',
                name:'',
                keywords:'',//这是搜索的关键字
                
                list :[
                    {id: 1,name: '奔驰',ctime: new Date() },
                    {id: 2,name: '宝马',ctime: new Date() }
            ]
            },
            methods: {
                add(){
                    //console.log("ok")
                    var car={ id:this.id,name:this.name,ctime: new Date()}
                    this.list.push(car)
                    this.name=this.id=''
                },
                del(id){
                    //第一种方法删除
                    //根据ID删除数据
                    //如何根据ID来删除索引
                    //找到索引，直接调用数组的splice方法
                  /*  this.list.some((item,i)=>{
                        if (item.id==id){
                            this.list.splice(i,1)
                            return true;
                        }

                    });
                    */
                   //第二种删除方法
                   var index=this.list.findIndex(item=>{
                       if(item.id==id){
                           return true;
                       }
                   })
                   //console.log(index)
                   this.list.splice(index,1)
                },
                searchByName(){
                console.log("ok")
                let result=[]
                for(let i=0;i<this.list.length;i++){
                  if(this.list[i].name.indexOf(this.keywords)!=-1){
                      result.push(this.list[i])
                  }
                }
                return result
            } 
            }
            
            /* search(keywords){//根据关键字进行数据的搜索
                var newList=[]
                this.list.forEach(item=>{
                    if(item.name.indexOf(keywords)!=-1){
                        newList.push(item)
                    }
                })
                return newList
            
            } */
            


        
        });





    </script>
    
</body>
</html>